/*
 * Copyright (C) 2010-2011 sunjumper@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package mfinder.util;

/**
 * 字符串工具类。
 */
public class StringUtil {

    /**
     *
     * 判断字符串是否为空。
     *
     * @param str 原字符串。
     *
     * @return 字符串为null或者为空则返回true。
     */
    public static boolean isNull(String str) {
        return str == null || str.isEmpty();
    }

    /**
     *
     * 去除字符串首尾的空格和特定字符。
     *
     * @param src 原字符串。
     * @param ch 指定字符。
     *
     * @return 去除首尾空格和指定字符后的字符串。
     */
    public static String trim(String src, char ch) {
        int begin = 0;
        int end = src.length() - 1;
        while (begin < end) {
            char c = src.charAt(begin);
            if (c == ' ' || c == ch) {
                begin++;
            } else {
                break;
            }
        }
        while (end >= begin) {
            char c = src.charAt(end);
            if (c == ' ' || c == ch) {
                end--;
            } else {
                break;
            }
        }
        return src.substring(begin, end + 1);
    }
}
